import Vue 				from 'vue'
import util 			from '../../util'
import axios 			from 'axios'
import { List,Toast } 	from 'vant'
Vue.use(List,Toast)

export default {
	name: 'history',
	data(){
		return {
			pageState:1,		//1:购买订单   2:出售订单
			pageIndex:1,		//页数
			name:'USDT',
			list:[],
			loading:false,
			finished:false,
			loadError:false,
            offset:200,
            isNigeria: 0,           //是否尼日利亚版本
			iconShown:false,		//是否显示币种选择弹窗
			iconList:[
				{ name: 'USDT',color:'' },
				{ name: 'BTC',color:'' },
			],
		}
	},
	computed:{
		topDistance(){
			//容器与顶部距离
			let clientWidth = document.documentElement.clientWidth;
			let _rem = clientWidth >= 750 ? 100 : 100 * (clientWidth / 750);
			return 'top:'+(0.5*_rem+100)+'px';
		},
	},
	methods:{
		choiceIcon(){
			//弹窗显示币种选择
			this.iconList.map(item => {
				item.color = this.name==item.name?'#0080FF':'';
			})
			this.iconShown = true;
		},
		selectIcon(item){
			//选择币种
			this.name = item.name;
			this.iconShown = false;
			this.pageIndex = 1;
			this.list = [];
			this.load();
		},
		getCoinsList(){
			//拉取币种配置
			util.fetch(`${util.ajaxUrl}/site/spot-currency-list`,{
				app_id:1
			}).then(res => {
				if(res.retCode === 0){
					let iconList = [];
					res.retData.map(item => {
						iconList.push({
							name:item.symbol,
							color:''
						})
					})
					this.iconList = iconList;
				}else{
					Toast(res.retMsg || this.$t('noError'))
				}
			}).catch(error => {
				console.log(error)
			})
		},
		load(){
			this.loading = true;
			let self = this;
			setTimeout(()=>{
				util.fetch(`${util.ajaxUrl}/transfer/partner-list`,{
					token:self.token || window.token || util.getToken(),
					page:self.pageIndex,
					currency_id:self.name || 'USDT'
				}).then(res => {
					self.loading = false;
					if(res.retCode === 0){
						self.pageIndex++;
						//修改时间格式
						res.retData.list.map(item => {
							item.date = util.transTime2(item.create_time);
							item.amount = util.toNonExponential(item.amount_e8/100000000);
							item.fromName = item.from_id==1?self.$t('transfer-key18'):item.from_id == 2?self.$t('transfer-key7'):item.from_id==3?self.$t('transfer-key6'):item.from_id==4?self.$t('transfer-key19'):item.from_id==5?self.$t('transfer-key20'):item.from_id==6?self.$t('transfer-key6'):item.from_id==7?self.$t('transfer-key36'):self.$t('transfer-key28');
							item.toName = item.to_id==1?self.$t('transfer-key18'):item.to_id == 2?self.$t('transfer-key7'):item.to_id==3?self.$t('transfer-key6'):item.to_id==4?self.$t('transfer-key19'):item.to_id==5?self.$t('transfer-key20'):item.to_id==6?self.$t('transfer-key6'):item.to_id==7?self.$t('transfer-key36'):self.$t('transfer-key28');
							//移除英文版的“Account”关键词
							item.fromName = item.fromName.indexOf('Account') > -1?item.fromName.replace(' Account',''):item.fromName;
							item.toName = item.toName.indexOf('Account') > -1?item.toName.replace(' Account',''):item.toName;
							// item.state = '0';
						})
						self.list = self.list.concat(res.retData.list);
						if(!res.retData || res.retData.list && res.retData.list.length < 20){
							self.finished = true;
						}
					}else{
						self.loadError = true;
						Toast(res.retMsg || self.$t('noError'))
					}
				}).catch(error => {
					self.loading = false;
					console.log(error)
				})
			},500)
		},
		goBack(){
			util.navigateBack(-1,this)
		},
		setStatusBarStyle(){
			util.callHandler('setStatusBarStyle',{
				style:'dark'
			})
		}
	},
	created() {
        if(this.$route.query.name) this.name = this.$route.query.name;
        let isNigeria = this.$route.query.isNigeria
        if(isNigeria == 1) {
            this.isNigeria = isNigeria
            this.iconList = [{ name: 'USDT',color:'' }]
        }
		util.waitSDKReady().then(token => {
			this.token = token;
			this.getCoinsList();
		}).catch((e) => {
			console.log('获取token失败')
		})
		setTimeout(()=>{
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				localStorage.removeItem('token');
			})
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				window.location.reload();
			})
			this.setStatusBarStyle();
		},300)
    }
}